define(['bootstrap_dialog','common/js/boss-frame-angular-dev-0.1'],function(BootstrapDialog){
	serviceModule.factory("CommonRestService",function($window, $http, $q,MaiDialog){
		var backCode = 200;
		function handleSuccess(dataResult, status,deferred){
			var msg = dataResult.code?dataResult.msg:dataResult;
			if(!dataResult.code || dataResult.code!= 200){
				if(msg && msg.indexOf("images/favicon") >= 0 ){
					if(!dataResult.code || dataResult.code == undefined){
						if(backCode  == 503){
							console.log("have already producer code by 503 , wait 3 seconds go to login.jsp");
							return;
						}
						backCode = 503;
						console.log("返回信息异常，登陆超时，3秒钟后自动跳转至登陆页面");
						MaiDialog.error("登陆超时，3秒钟后自动跳转至登陆页面");
						var url = window.rootPath + "/view/main/login";
						if(sessionStorage){
							if(sessionStorage.getItem("alert") !=null){
								sessionStorage.removeItem("alert");
							}
						}
						window.setTimeout("location.href='"+url+"'",3000)
						return;
					}
				}else{
					if(backCode == 502){
						console.log("返回值502，常规登陆超时，3秒钟后自动跳转至登陆页面");
						console.log("have already producer code by 502 , wait 3 seconds go to login.jsp");
						return;
					}
					if(dataResult.code && dataResult.code == 502){
						console.log("返回值502，常规登陆超时，3秒钟后自动跳转至登陆页面");
						backCode = dataResult.code;
						MaiDialog.error("登陆超时，3秒钟后自动跳转至登陆页面");
						var url = window.rootPath + "/view/main/login";
						if(sessionStorage){
							if(sessionStorage.getItem("alert") !=null){
								sessionStorage.removeItem("alert");
							}
						}
						window.setTimeout("location.href='"+url+"'",3000)
						return;
					}else{
						MaiDialog.error(dataResult.msg);
					}
				}

				//仍然可以交给开发人员，可以自定义错误处理
				deferred.reject(dataResult);
				return;
			}
			deferred.resolve(dataResult.data);
		}

		function handlerError(data, status,deferred){
			MaiDialog.error("网络异常，请稍后再试");
			//仍然可以交给开发人员，可以自定义错误处理
			deferred.reject(data);
		}

		return {
			query: function(shortUrl,requestParams,useUrlParam){
				if(!requestParams){
					requestParams = {};
				}
				var urlParams = requestParams;
				if(false === useUrlParam){
					urlParams = {};
				}

				var deferred = $q.defer();

				//if(typeof requestParams == "object"){
				$http({
					method:globalVar.methodPost,
					url:rootPath + shortUrl,
					params:urlParams,
					data :requestParams
				}).
				success(function(dataResult, status) {
					handleSuccess(dataResult,status,deferred);
				}).
				error(function(data, status) {
					handlerError(data,status,deferred);
				});
				//}

				return deferred.promise;
			},
			queryResponseEntity : function(shortUrl,requestParams,requestMethod){
				if(!requestParams){
					requestParams = {};
				}

				var reqMethod = globalVar.methodPost;
				if(requestMethod){
					reqMethod = requestMethod;
				}

				var deferred = $q.defer();

				//if(typeof requestParams == "object"){
				$http({
					method:reqMethod,
					url:rootPath + shortUrl,
					params:requestParams,
					data :requestParams
				}).
				success(function(responseEntity, status) {
					var dataResult = null;

					if(responseEntity.code){
						dataResult = responseEntity;
					}else{
						dataResult = {
							data : responseEntity,
							code: 200
						};
					}

					handleSuccess(dataResult,status,deferred);
				}).
				error(function(data, status) {
					handlerError(data,status,deferred);
				});

				return deferred.promise;
			}
		};
	});

	serviceModule.factory("PageDataStorage",function($rootScope,MaiDialog){
		//TODO : 此处考虑不适用rootScope
		$rootScope.dataStorage = {};
		return {
			count:0,
			saveItem: function(key,val){

				$rootScope.dataStorage[key] = val;

				this.count++;
			},
			getItem:function(key){
				return 	$rootScope.dataStorage[key];
			},
			clearItem:function(key){
				delete $rootScope.dataStorage[key];
				this.count--;
			},
			clearAll:function(){
				$rootScope.dataStorage = {};
				this.count = 0
			}
		};
	});

	serviceModule.factory('MaiDialog',function(){
		var okLabel = '确定',
			cancelLabel = '取消',
			defaultTitle = '温馨提示';

		var pleaseWaitDiv = $('<div class="modal hide" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false"><div class="modal-header"><h1>Processing...</h1></div><div class="modal-body"><div class="progress progress-striped active"><div class="bar" style="width: 100%;"></div></div></div></div>');

		return {
			alert : function(){
				var options = {
					callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
				};
				var defaultOptions = {
					size : BootstrapDialog.SIZE_SMALL,
					type : 'modal-confirm',
					title : defaultTitle,
					buttonLabel : okLabel,
					closable: false,
					draggable: false,
					animate: false,
					data: {
						callback: options.callback
					},
					onhide: function(dialog) {
						!dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
					},
					buttons: [{
						label: okLabel,
						cssClass : 'btn-modal-ok',
						action: function(dialog) {
							dialog.setData('btnClicked', true);
							typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
							dialog.close();
						}
					}]
				};

				//设置选项配置
				if (arguments[0] && typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
					options = $.extend(true, defaultOptions, arguments[0]);
				} else {
					options = $.extend(true, defaultOptions, {
						message: arguments[0],
						callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
					});
				}

				return new BootstrapDialog(options).open();
			},
			error : function(){
				var options = {
					callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
				};
				var defaultOptions = {
					size : BootstrapDialog.SIZE_SMALL,
					type : 'modal-alert',
					title : defaultTitle,
					closable: false,
					draggable: false,
					animate: false,
					data: {
						callback: options.callback
					},
					onhide: function(dialog) {
						!dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
					},
					buttons: [{
						label: okLabel,
						cssClass : 'btn-modal-alert',
						action: function(dialog) {
							dialog.setData('btnClicked', true);
							typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
							dialog.close();
						}
					}]
				};

				//设置选项配置
				if (arguments[0] && typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
					options = $.extend(true, defaultOptions, arguments[0]);
				} else {
					options = $.extend(true, defaultOptions, {
						message: arguments[0],
						callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
					});
				}

				return new BootstrapDialog(options).open();
			},
			confirm : function(){
				var options = {
					callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
				};
				var defaultOptions = {
					size: BootstrapDialog.SIZE_SMALL,
					type: 'modal-confirm',
					title: defaultTitle,
					closable: false,
					draggable: false,
					animate: false,
					data: {
						callback: options.callback
					},
					buttons: [{
						label: okLabel,
						cssClass: 'btn-modal-ok',
						action: function(dialog) {
							typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
							dialog.close();
						}
					},{
						label: cancelLabel,
						cssClass: 'btn-modal-cancel',
						action: function(dialog) {
							typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
							dialog.close();
						}
					}]
				};

				if (arguments[0] && typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
					options = $.extend(true, defaultOptions, arguments[0]);
				} else {
					options = $.extend(true, defaultOptions, {
						message: arguments[0],
						callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
					});
				}

				return new BootstrapDialog(options).open();
			},
            // 参考BootstrapDialog的用法，http://nakupanda.github.io/bootstrap3-dialog/
			show : function(options) {
				return new BootstrapDialog(options).open();
			},
			showWait : function(options) {
				pleaseWaitDiv.modal();
			},
			hideWait : function(){
				pleaseWaitDiv.modal('hide');
			}
		}
	});

});